package com.inossem.allotin.adapter.input;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inossem.allotin.R;
import com.inossem.wms.vo.auth.DicStockLocationAndWhTreeVo;
import com.inossem.wms.vo.auth.DicStockLocationTreeVo;

import java.util.List;

import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_TWO;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ZERO;

/**
 * @author LinH
 */
public class TransportInputLocationAdapter extends RecyclerView.Adapter<TransportInputLocationAdapter.ViewHolder> {
    private DicStockLocationAndWhTreeVo dicStockLocationAndWhTreeVo;
    private Context context;
    private List<DicStockLocationTreeVo> locationList;
    private OnClickListener onClickListener = null;

    public void initDate(DicStockLocationAndWhTreeVo dicStockLocationAndWhTreeVo) {
        locationList = dicStockLocationAndWhTreeVo.getFactoryList().get(0).getLocationList();
        dicStockLocationAndWhTreeVo.getFactoryList().get(0).getLocationList();
        this.dicStockLocationAndWhTreeVo = dicStockLocationAndWhTreeVo;
        notifyDataSetChanged();
    }

    public DicStockLocationAndWhTreeVo getData() {
        return dicStockLocationAndWhTreeVo;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        context = viewGroup.getContext();
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_transport_input_location, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (i % STATUS_TWO == STATUS_ZERO) {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_white));
        } else {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_bg));
        }
        viewHolder.tv1.setText(locationList.get(i).getLocationCode().concat(context.getString(R.string.common_line_1)).concat(locationList.get(i).getLocationName()));
        viewHolder.layout.setOnClickListener(v -> onClickListener.onButtonClick(
                locationList.get(i).getWhCode(),
                locationList.get(i).getWhName(),
                locationList.get(i).getLocationCode(),
                locationList.get(i).getLocationName()));
    }

    @Override
    public int getItemCount() {
        return locationList.size();
    }

    public void setOnClickListener(OnClickListener l) {
        this.onClickListener = l;
    }

    public interface OnClickListener {
        void onButtonClick(String wc, String wn, String lc, String ln);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv1;
        LinearLayout layout;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.tv_1);
            layout = itemView.findViewById(R.id.layout);
        }
    }
}